package com.winchain.chainbill.bill.biz.discount.service.impl;

import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.BillTranEvent;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctRevCallbackChdSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctRevCallbackDctSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctRevCallbackPrntSubscriber;
import com.winchain.chainbill.bill.biz.discount.rx.BillDctRevRollbackPrntSubscriber;
import com.winchain.chainbill.bill.biz.discount.service.BillDctEventService;
import com.winchain.chainbill.bill.biz.discount.service.BillDctPmtService;
import com.winchain.chainbill.chain.callback.CallbackService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import rx.Observable;

@Service("billDctRevCallbackService")
public class BillDctRevCallbackServiceImpl implements CallbackService {
	@Autowired
	private BillDctEventService billDctEventService;
	@Autowired
	private BillDctRevCallbackPrntSubscriber billDctRevCallbackPrntSubscriber;
	@Autowired
	private BillDctRevCallbackChdSubscriber billDctRevCallbackChdSubscriber;
	@Autowired
	private BillDctRevRollbackPrntSubscriber billDctRevRollbackPrntSubscriber;
	@Autowired
	private BillDctPmtService billDctPmtService;
	@Autowired
	private BillDctRevCallbackDctSubscriber billDctRevCallbackDctSubscriber;
	@Autowired
	private CbRecService cbRecService;

	public String getType() {
		return "cb_dct_rev";
	}

	public void confirm(AcctTxRec tran) {
		String billId = tran.getBizId();
		CbRec rec = (CbRec) this.cbRecService.selectByKey(billId);
		if ("S000".equals(rec.getBillState())) {
			billId = rec.getPrntId();
		}
		BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(billId, true);
		event.setTran(tran);

		Observable<BillTranEvent<CbDct>> observers = Observable.just(event);
		observers.subscribe(this.billDctRevCallbackPrntSubscriber);
		observers.subscribe(this.billDctRevCallbackChdSubscriber);

		observers.subscribe(this.billDctRevCallbackDctSubscriber);
		if ("review".equals(SpringContextUtil.getProperty("discount.paid.mode"))) {
			if ((event.getChildren() == null) || (event.getChildren().isEmpty())) {
				if (!prefinancing((CbDct) event.getBill().getSource())) {
					this.billDctPmtService.pmt(null, billId);
				}
			} else {
				List<Bill<CbDct>> bills = (List<Bill<CbDct>>) event.getChildren();
				for (Bill<CbDct> bill : bills) {
					if ((((CbDct) bill.getSource()).getDctId() != null) && (!prefinancing((CbDct) bill.getSource()))) {
						this.billDctPmtService.pmt(null, ((CbDct) bill.getSource()).getBillId());
					}
				}
			}
		}
	}

	public void cancel(AcctTxRec tran) {
		String billId = tran.getBizId();
		CbRec rec = (CbRec) this.cbRecService.selectByKey(billId);
		if ("S000".equals(rec.getBillState())) {
			billId = rec.getPrntId();
		}
		BillTranEvent<CbDct> event = this.billDctEventService.getBillTranEvent(billId, true);
		event.setTran(tran);

		Observable<BillTranEvent<CbDct>> observers = Observable.just(event);
		observers.subscribe(this.billDctRevRollbackPrntSubscriber);
	}

	private boolean prefinancing(CbDct bill) {
		boolean prefinancing = false;

		String revDate = DateUtil.getSystemDate();
		String dctDate = bill.getDctDate();
		if (SysDateUtil.compare(dctDate, revDate) == 1) {
			prefinancing = true;
		}
		return prefinancing;
	}
}
